package com.bw.module_video_detail.test_gl_surfaceview.filter

import android.opengl.GLSurfaceView
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
import com.bw.module_video_detail.R


class FilterActivity : AppCompatActivity() {
    private lateinit var glSurfaceView: GLSurfaceView
    private lateinit var filterSpinner: Spinner

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_filter)

        glSurfaceView = findViewById<GLSurfaceView>(R.id.glSurfaceView)
        filterSpinner = findViewById<Spinner>(R.id.filterSpinner)

        // 初始化GLSurfaceView
        glSurfaceView.setEGLContextClientVersion(2)
        val renderer = FilterRenderer(this)
        glSurfaceView.setRenderer(renderer)

        // 滤镜选择器
        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.filter_types,
            android.R.layout.simple_spinner_item
        )
        filterSpinner.setAdapter(adapter)
        filterSpinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                renderer.setCurrentFilter(position) // 切换滤镜
                glSurfaceView.requestRender()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })
    }

    override fun onPause() {
        super.onPause()
        glSurfaceView!!.onPause()
    }

    override fun onResume() {
        super.onResume()
        glSurfaceView!!.onResume()
    }
}